package com.kaluli.project.utils.nanshaxml;
/**
 * 此类用于组装返利网Xml的推送报文
 * 此类的工作环境是多线程
 * @author zyzowen1944
 *  Date 2016-2-2
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.dom4j.*;
import org.dom4j.io.*;
public  class XmlFanli {
	private static XmlFanli xmlBuilder=null;
	private static Document document = null;
	public final static String ROOT="item";
	
	public final static String ORDER_ROOT="orders";
	public final static String ORDER="order";
	public final static String S_ID="s_id";
	public final static String ORDER_ID_PARENT="order_id_parent";
	public final static String ORDER_ID="order_id";
	public final static String ORDER_TIME="order_time";
	public final static String UID="uid";
	public final static String UNAME="uname";
	public final static String TC="tc";
	public final static String PAY_TIME="pay_time";
	public final static String STATUS="status";
	public final static String LOCKED="locked";
	public final static String LASTMOD="lastmod";
	public final static String IS_NEWBUYER="is_newbuyer";
	public final static String PLATFORM="platform";
	public final static String CODE="code";
	public final static String REMARK="remark";
	public final static String PRODUCTS="products";
	public final static String PRODUCT="product";
	public final static String PID="pid";
	public final static String TITLE="title";
	public final static String CATEGORY="category";
	public final static String CATEGORY_TITLE="category_title";
	public final static String URL="url";
	public final static String NUM="num";
	public final static String PRICE="price";
	public final static String REAL_PAY_FEE="real_pay_fee";
	public final static String REFUND_NUM="refund_num";
	public final static String COMMISSION_NUM="commission";
	public final static String COMM_TYPE="comm_type";
	
	
	/**
	 * 创建返利网Xml组装器
	 * @param root
	 * @param head
	 * @param body
	 * @return
	 */
	public  static XmlFanli getInstance(){
		synchronized(XmlFanli.class){  //避免多线程调用该方法时串行执行，效率低下
			//if(xmlBuilder==null){
				xmlBuilder =  new XmlFanli();
				
			//}
		}
		return xmlBuilder;
	}
	
	public XmlFanli(){
		document = DocumentFactory.getInstance().createDocument();
		document.addElement(ROOT);
		Element root = document.getRootElement();
	}
	
	public XmlFanli(String type) {
		document = DocumentFactory.getInstance().createDocument();
		document.addElement(ORDER_ROOT);
	}

	public static XmlFanli getInstance(String type){
		synchronized(XmlBuilder.class){  //避免多线程调用该方法时串行执行，效率低下
			xmlBuilder = new XmlFanli(type);
		}
		return xmlBuilder;
	}

	public void addBody(String key,Object value,boolean isleaf,String parentName){
		String xpath = "/"+parentName;
		@SuppressWarnings("unchecked")
		List<Element> composites = document.selectNodes(xpath);
		Element body = composites.get(composites.size()-1);
		if(isleaf){ //如果是叶子节点，同时创建Tag和Value
			if(value.getClass().getName().equals(String.class.getName())){
				body.addElement(key).addText(value.toString());
			}else{
				//未实现 
			}
		}else{  //如果是子节点
			body.addElement(key);
		}
	}
	
	public String toString(){
		return document.asXML();
	}
	public static void main(String s[]){
		/*XmlFanli xml = XmlFanli.getInstance();
		xml.addBody("id", "12", true,ROOT);
		xml.addBody("title","23",true,ROOT);
		xml.addBody("category", "", true, ROOT); 
		xml.addBody("url","", true, ROOT);   
		xml.addBody("url_wap","", true, ROOT);   
		xml.addBody("price","", true, ROOT);   
		xml.addBody("promotion_price","", true, ROOT);   
		xml.addBody("wap_price","", true, ROOT);   
		xml.addBody("detail","", true, ROOT);   
		xml.addBody("status", 1, true	, ROOT);
		xml.addBody("pic_main", "", false, ROOT);
		xml.addBody("img", "", false, ROOT+"/pic_main");  
		xml.addBody("url", "<![CDATA[111.jpg]>", true, ROOT+"/pic_main/img");
		xml.addBody("size", "400*400", true, ROOT+"/pic_main/img");
		xml.addBody("pic_extra", "", false, ROOT);
		xml.addBody("img", "", false, ROOT+"/pic_extra");  
		xml.addBody("url", "<![CDATA[33.jpg]>", true, ROOT+"/pic_extra/img");
		xml.addBody("size", "400*400", true, ROOT+"/pic_extra/img");
		System.out.println(xml.toString());*/
		SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
		String s2 = sFormat.format(new Date());
		System.out.print(s2);
	}
	
}
